﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace SpaceWindow
{
    class SpaceWindow : Window
    {
        public static readonly DependencyProperty SpaceProperty;

        public int Space
        {
            get
            {
                return (int)GetValue(SpaceProperty);
            }

            set
            {
                SetValue(SpaceProperty, value);
            }
        }

        static SpaceWindow()
        {
            FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata();
            metadata.Inherits = true;

            SpaceProperty = SpaceButton.SpaceProperty.AddOwner(typeof(SpaceWindow));
            SpaceProperty.OverrideMetadata(typeof(SpaceWindow), metadata);
        }

        public SpaceWindow()
        {
            this.Title = "Set Space Property";
            this.SizeToContent = SizeToContent.WidthAndHeight;
            this.ResizeMode = ResizeMode.CanMinimize;
            int[] iSpace = new int[] { 0, 1, 2 };

            Grid grid = new Grid();
            this.Content = grid;

            for (int i = 0; i < 2; i++)
            {
                RowDefinition row = new RowDefinition();
                row.Height = GridLength.Auto;
                grid.RowDefinitions.Add(row);
            }

            for (int i = 0; i < iSpace.Length; i++)
            {
                ColumnDefinition col = new ColumnDefinition();
                col.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(col);
            }

            for (int i = 0; i < iSpace.Length; i++)
            {
                SpaceButton btn = new SpaceButton();
                btn.Text = "Set window Space to" + iSpace[i];
                btn.Tag = iSpace[i];
                btn.HorizontalAlignment = HorizontalAlignment.Center;
                btn.VerticalAlignment = VerticalAlignment.Center;
                btn.Click += this.Btn_Click;
                grid.Children.Add(btn);
                Grid.SetRow(btn, 0);
                Grid.SetColumn(btn, i);

                btn = new SpaceButton();
                btn.Text = "Set button Space to" + iSpace[i];
                btn.Tag = iSpace[i];
                btn.HorizontalAlignment = HorizontalAlignment.Center;
                btn.VerticalAlignment = VerticalAlignment.Center;
                btn.Click += this.ButtonPropertyOnClick;
                grid.Children.Add(btn);
                Grid.SetRow(btn, 1);
                Grid.SetColumn(btn, i);

            }
        }

        private void ButtonPropertyOnClick(object sender, RoutedEventArgs e)
        {
            SpaceButton btn = e.Source as SpaceButton;
            btn.Space = (int)btn.Tag;
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            SpaceButton btn = e.Source as SpaceButton;
            this.Space = (int)btn.Tag;
        }
    }
}
